package cn.momoshenchi.controller;

import cn.momoshenchi.pojo.Student;
import cn.momoshenchi.service.StudentService;
import cn.momoshenchi.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.Info;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/3/23 - 14:06
 */
@Controller
@Api("学生信息")
public class StudentController
{
    @Autowired
    private StudentService studentService;

    public  ResultInfo success()
    {
        return  new ResultInfo();
    }

    @Operation(summary = "主页")
    @GetMapping("home")
    public String addPage()
    {
        return "/view/studentTable.html";
    }

    @Operation(summary="查询学生信息列表")
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> list(int page,int size)
    {
        return studentService.selectAll(page,size);
    }

    @Operation(summary="通过主键查询某个学生信息")
    @PreAuthorize("hasAuthority('student:select')")
    @GetMapping("student/{id}")
    @ResponseBody
    public Map<String, Object> select(@PathVariable Integer id)
    {
       Student student = studentService.selectById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data",student);
        return map;
    }
    @Operation(summary="添加学生信息")
    @PreAuthorize("hasAuthority('student:add')")
    @PostMapping("student")
    @ResponseBody
    public ResultInfo add(@Valid  @RequestBody  Student student)
    {
        studentService.add(student);
        return  success();
    }

    @Operation(summary="修改学生信息")
    @PutMapping("student")
    @PreAuthorize("hasAuthority('student:update')")
    @ResponseBody
    public ResultInfo update( @RequestBody Student student)
    {
        studentService.update( student);
        return  success();
    }

    @Operation(summary="通过主键删除学生信息")
    @DeleteMapping("student/{id}")
    @PreAuthorize("hasAuthority('student:delete')")
    @ResponseBody
    public ResultInfo delete(@PathVariable  Integer id)
    {
        studentService.delete(id);
        return  success();
    }
}
